import scrapy

from movie_spider.items import DoubanItem


class DoubanSpider(scrapy.Spider):
    name = "douban"
    allowed_domains = ["movie.douban.com"]
    start_urls = ["https://movie.douban.com/top250"]

    def parse(self, response):
        movies = response.xpath("//ol[@class='grid_view']/li")
        for movie in movies:
            doubanItem = DoubanItem()
            doubanItem["title"] = movie.xpath("./div/div[@class='info']/div[1]/a/span[1]/text()").get()
            text1 = movie.xpath("./div/div[@class='info']/div[2]/p/text()[1]").get().strip()
            doubanItem["director"] = text1.split(" ")[1]
            text2 = movie.xpath("./div/div[@class='info']/div[2]/p/text()[2]").get().strip().replace("\xa0", "")
            doubanItem["year"] = text2.split("/")[0][0:4]
            doubanItem["area"] = text2.split("/")[1].split(" ")[0]
            doubanItem["category"] = text2.split("/")[2].split(" ")
            doubanItem["rate"] = movie.xpath("./div/div[@class='info']/div[2]/div/span[@class='rating_num']/text()").get()
            text3 = movie.xpath("./div/div[@class='info']/div[2]/div/span[4]/text()").get()
            doubanItem["review"] = text3[0:text3.index("人")]
            yield doubanItem

        for i in range(1, 11):
            yield response.follow("https://movie.douban.com/top250?start=" + str(25 * i), self.parse)
